//
//  ImageTableViewCell.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "ImageTableViewCell.h"
#import "SmileModel.h"
#import "LHMGetHeightTool.h"
#import "UIImageView+AFNetworking.h"
#import "LHMScanImageView.h"

@interface ImageTableViewCell ()

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIImageView *myImageView;

@end

@implementation ImageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview: _titleLabel];
        
        self.myImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_myImageView];
        
    }
    return self;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat titleHeight = [LHMGetHeightTool heightWith:self.smileModel.text];
    self.titleLabel.frame = CGRectMake(10, 35, self.contentView.bounds.size.width - 20, titleHeight);
    
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    
    NSArray *imageArray = [self.smileModel.image objectForKey:@"big"];
    
    NSString *url = [imageArray objectAtIndex:0];
    
    CGFloat imageHeight = [LHMGetHeightTool twoHeightWith:url];
    
    self.myImageView.frame = CGRectMake(10, titleHeight + 35, self.contentView.bounds.size.width - 20, imageHeight);
    self.myImageView.backgroundColor = [UIColor whiteColor];
    self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];
    [self.myImageView addGestureRecognizer:tapGes];
    self.myImageView.userInteractionEnabled = YES;
    
}

-(void)setSmileModel:(SmileModel *)smileModel {
    
    if (_smileModel != smileModel) {
        _smileModel = smileModel;
    }
    
    self.titleLabel.text = smileModel.text;
    
    NSArray *imageArray = [smileModel.image objectForKey:@"big"];
    
    NSURL *url = [NSURL URLWithString:[imageArray objectAtIndex:0]];
    
    [self.myImageView setImageWithURL:url];
    
}

-(void)scanBigImageClick:(UITapGestureRecognizer *)tap {
    
    NSLog(@"点击图片");
    UIImageView *clickedImageVIew = (UIImageView *)tap.view;
    [LHMScanImageView scanBigImageWithImageVIew:clickedImageVIew];
    
}

@end
